package com.tiny.mirror.service.product.application.query;

import com.api.tiny.mirror.service.product.request.PageAbilityRequest;
import com.api.tiny.mirror.service.product.request.ParamsRequest;
import com.api.tiny.mirror.service.product.response.AbilityVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiny.mirror.basic.common.code.CommonStatusCode;
import com.tiny.mirror.basic.web.dto.response.BasePageResponse;
import com.tiny.mirror.basic.web.exception.BusinessException;
import com.tiny.mirror.service.product.application.convert.AbilityConvert;
import com.tiny.mirror.service.product.enums.AbilityStatusEnum;
import com.tiny.mirror.service.product.enums.AbilityTypeEnum;
import com.tiny.mirror.service.product.infrastructure.entity.db.AbilityEntity;
import com.tiny.mirror.service.product.infrastructure.repository.db.AbilityMapper;
import java.util.Objects;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Author: liujsh
 * @Description:
 * @Date: Create in 2023/9/27
 */
@Service
public class AbilityQryService {

  @Resource
  private AbilityMapper abilityMapper;

  public BasePageResponse<AbilityVO> page(PageAbilityRequest request) {
    QueryWrapper<AbilityEntity> queryWrapper = new QueryWrapper<>();
    String name = request.getName();
    if (StringUtils.isNotBlank(name)) {
      queryWrapper.lambda().like(AbilityEntity::getName, name);
    }
    AbilityTypeEnum type = request.getType();
    if (type != null) {
      queryWrapper.lambda().like(AbilityEntity::getType, type);
    }
    AbilityStatusEnum status = request.getStatus();
    if (status != null) {
      queryWrapper.lambda().eq(AbilityEntity::getStatus, status);
    }
    ParamsRequest params = request.getParams();
    if (params != null) {
      if (params.getBeginCreateTime() != null) {
        queryWrapper.lambda().ge(AbilityEntity::getCreateTime, params.getBeginCreateTime());
      }
      if (params.getEndCreateTime() != null) {
        queryWrapper.lambda().le(AbilityEntity::getCreateTime, params.getEndCreateTime());
      }
      if (params.getBeginUpdateTime() != null) {
        queryWrapper.lambda().ge(AbilityEntity::getUpdateTime, params.getBeginUpdateTime());
      }
      if (params.getEndUpdateTime() != null) {
        queryWrapper.lambda().le(AbilityEntity::getUpdateTime, params.getEndUpdateTime());
      }
    }
    IPage<AbilityEntity> iPage = abilityMapper.selectPage(
        new Page<>(request.getPage(), request.getPageSize()), queryWrapper
    );
    return BasePageResponse.success(
        iPage.getPages(), iPage.getSize(),
        AbilityConvert.convertEntityListToAbilityVOList(iPage.getRecords()),
        iPage.getTotal()
    );
  }

  public AbilityVO getInfo(Long id) {
    AbilityEntity entity = abilityMapper.selectById(id);
    if (Objects.isNull(entity)) {
      throw new BusinessException(CommonStatusCode.DATA_NOT_EXIST.args("app:" + id));
    }
    return AbilityConvert.convertToAbilityVO(entity);
  }
}
